/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.posplus.losspreventiondataimporter.svt;

import com.posplus.lossprevention.dao.StoresDAO;
import com.posplus.lossprevention.mapping.Cashiers;
import com.posplus.lossprevention.mapping.Stores;
import com.posplus.strack.mapping.Employees;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author ScottW
 */
public class CashiersMapper {

    public static List<Cashiers> svtToLp(List<Employees> employees, StoresDAO storeDAO) {
        List<Cashiers> lpCashiers = new ArrayList<Cashiers>();
        List<Stores> lpStores = storeDAO.findAll();
        Map<String, Stores> storesMap = listToMap(lpStores);
        
        Iterator<Employees> iter = employees.iterator();
        while(iter.hasNext()){
            Employees employee = iter.next();
            
            try{
                Stores store = storesMap.get("8765");
                if(store == null)
                    throw new RuntimeException("Store code: 8765 not found");
                
                Cashiers cashier = new Cashiers(store, "" + employee.getEmpNo(), employee.getFirstName(), employee.getLastName(), employee.getJobTitle(), null, null, null);
                lpCashiers.add(cashier);
            }catch(RuntimeException ex){
                Logger.getLogger(CashiersMapper.class).warn(ex.getMessage());
            }
        }
        
        
        return lpCashiers;
    }

    private static Map<String, Stores> listToMap(List<Stores> lpStores) {
        Map<String, Stores> result = new HashMap<String, Stores>();
        
        Iterator<Stores> iter = lpStores.iterator();
        while(iter.hasNext()){
            Stores next = iter.next();
            result.put(next.getCode(), next);
        }
        
        return result;
    }
    
}
